/**
 * @(#) TotalIncomProxy.as
 *
 * @date		2009. 04. 23.
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				특별공제 보험료 입력 관리 
 */
package com.skt.common_hr.yearadjustment.model
{

	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.vo.PremiumVO;
	import com.wcl.dataloader.DataLoaderFactory;
	import com.wcl.dataloader.IDataLoader;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class PremiumProxy extends CommonProxy
	{
		public static const NAME:String = "PremiumProxy";
		public var msgStr:String;
		[Bindable] public var premiumData:ArrayCollection = new ArrayCollection();

		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function PremiumProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = pernr;
			param["I_YEAGBN"] = "01";		//ITAB2 01:보험비, 02:교육비, 03:신용카드 비용), ITAB (06:기부금, 07:의료비)
			param["I_PROC_TYPE"] = "D";		//D:조회, S:저장
			loadData("Z_HR_0085_NEW", param, result, fault);
		}
		
		public function save(parnr:String, year:String, premiumArr:Array):void{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0085");
			/* for(var i:int  = 0 ; i < premiumArr.length; i++){
				trace("SEQNO : " + premiumArr[i].SEQNO);
				trace("DPTYP : " + premiumArr[i].DPTYP);
				trace("OBJPS : " + premiumArr[i].OBJPS);
				trace("REGNO : " + premiumArr[i].REGNO);
				trace("SUBTY : " + premiumArr[i].SUBTY);
				trace("AMT11 : " + premiumArr[i].AMT11);
				trace("AMT12 : " + premiumArr[i].AMT12);
				trace("AMT13 : " + premiumArr[i].AMT13);
				trace("AMT21 : " + premiumArr[i].AMT21);
				trace("AMT22 : " + premiumArr[i].AMT22);
				trace("AMT23 : " + premiumArr[i].AMT23);
				
				trace("==============================");
			} */
			var param:Object = new Object();
			param["I_YEAR"] = year;
			param["I_PERNR"] = parnr;
			param["I_YEAGBN"] = "01";		//ITAB2 01:보험비, 02:교육비, 03:신용카드 비용), ITAB (06:기부금, 07:의료비)
			param["I_PROC_TYPE"] = "S";		//D:조회, S:저장
			var table:Object = new Object();
			table["ITAB2"] = premiumArr;
			saveData("Z_HR_0085_NEW", param, table, saveResult, saveFault);
		}
		

		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to load premium proxy");
				premiumData = parsePremium(event.result.ITAB2);
				facade.sendNotification(YearAdjustmentFacade.LOAD_PREMIUM_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to load premium: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_PREMIUM_LIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in premium: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_PREMIUM_LIST_FAILED, this);
		}
		
		
		
		//저장 완료
		public function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to save premium proxy");
				facade.sendNotification(YearAdjustmentFacade.SAVE_PREMIUM_LIST_COMPLETE, this);
			}
			else {
				trace( "failed to save donation: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.SAVE_PREMIUM_LIST_FAILED, this);
			}
		}
		
		//저장실패
		private function saveFault(event:FaultEvent):void
		{
			trace("Fault message in premium: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.SAVE_PREMIUM_LIST_FAILED, this);
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 개인정보
		 * 
		 * @param
		 * @return 특별공제 보험료 Value Object
		 */
		private function parsePremium(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in arr){
				var vo:PremiumVO = new PremiumVO();
				vo.SEQNO	= rec["SEQNO"];
				vo.REGNO	= rec["REGNO"];
				vo.SUBTY	= rec["SUBTY"];
				vo.OBJPS	= rec["OBJPS"];
				vo.ENAME	= rec["ENAME"];
				vo.DPTYP	= rec["DPTYP"];
				vo.DPTYPT	= rec["DPTYPT"];
				vo.HNDID	= rec["HNDID"];
				vo.YEASUB	= rec["YEASUB"];
				vo.YEASTX	= rec["YEASTX"];
				vo.AMT11	= rec["AMT11"];
				vo.AMT12	= rec["AMT12"];
				vo.AMT13	= rec["AMT13"];
				vo.AMT21	= rec["AMT21"];
				vo.AMT22	= rec["AMT22"];
				vo.AMT23	= rec["AMT23"];
//shgoo 2010.01.11
				vo.DPTID	= rec["DPTID"];
//////////////////				
				
				returnArr.addItem(vo);
			}
			
			return returnArr;
		}
		
	}
}